package com.sqx.modules.reportBoss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.record.entity.Record;
import com.sqx.modules.reportBoss.entity.ReportBoss;
import com.sqx.modules.reportBoss.dao.ReportBossDao;
import com.sqx.modules.reportBoss.service.IReportBossService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 黑老板列表 服务实现类
 * </p>
 *
 * @author wuchang
 * @since 2023-07-25
 */
@Service
public class ReportBossServiceImpl extends ServiceImpl<ReportBossDao, ReportBoss> implements IReportBossService {

    @Autowired
    private UserService userService;

    @Override
    public Result insertReportBoss(ReportBoss reportBoss) {
        UserEntity userEntity = userService.getById(reportBoss.getUserId());
        reportBoss.setExamineStatus(0);
        reportBoss.setUserPhone(userEntity.getPhone());
        reportBoss.setUserName(userEntity.getUserName());
        if (reportBoss.getReportId() == null) {
            reportBoss.setCreateTime(LocalDateTime.now());
            return Result.upStatus(baseMapper.insert(reportBoss));
        } else {
            return Result.upStatus(baseMapper.updateById(reportBoss));
        }


    }

    @Override
    public Result deleteReportBoss(Long userId, Long reportId) {

        ReportBoss reportBoss = baseMapper.selectById(reportId);
        if (reportBoss == null) {
            return Result.error("信息不存在");
        } else {
            if (!reportBoss.getUserId().equals(userId)) {
                return Result.error("非法操作");
            } else {
                return Result.upStatus(baseMapper.deleteById(reportId));
            }
        }
    }

    @Override
    public IPage<ReportBoss> getReportBossList(Integer page, Integer limit, ReportBoss reportBoss) {
        Page<ReportBoss> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }

        return baseMapper.selectPage(pages, new QueryWrapper<>(reportBoss).orderByDesc("create_time"));
    }

    @Override
    public Result examineReportBoss(Long reportId, Integer examineStatus, String examineReason) {
        ReportBoss reportBoss = baseMapper.selectById(reportId);
        if (reportBoss == null) {
            return Result.error("举报信息不存在");
        } else {
            reportBoss.setExamineStatus(examineStatus);
            reportBoss.setExamineReason(examineReason);
            return Result.upStatus(baseMapper.updateById(reportBoss));
        }


    }
}
